package com.example.demo.exception;

import lombok.Getter;

/**
 * 业务异常（统一业务异常封装，支持错误码、消息、cause）
 *
 * @author chenhao
 * @date 2023-5-31 10:41
 */
@Getter
public class BizException extends RuntimeException {

    private final int status;
    /** 错误码 */
    private final int code;
    /** 错误信息 */
    private final String message;

    /**
     * 仅错误常量
     */
    public BizException(ErrorConstants error) {
        super(error.getMsgTemplate());
        this.status = error.getHttpStatusCode();
        this.code = error.getBizCode();
        this.message = error.getMsgTemplate();
    }

    /**
     * 错误常量+自定义消息
     */
    public BizException(ErrorConstants error, String message) {
        super(message != null ? message : error.getMsgTemplate());
        this.status = error.getHttpStatusCode();
        this.code = error.getBizCode();
        this.message = message != null ? message : error.getMsgTemplate();
    }

    /**
     * 错误常量+cause
     */
    public BizException(ErrorConstants error, Throwable cause) {
        super(error.getMsgTemplate(), cause);
        this.status = error.getHttpStatusCode();
        this.code = error.getBizCode();
        this.message = error.getMsgTemplate();
    }

    /**
     * 自定义错误码+消息
     */
    public BizException(int status, int code, String message) {
        super(message);
        this.status = status;
        this.code = code;
        this.message = message;
    }
}
